<template>
  <el-container class="reward-view panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">佣金明细</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
          <el-button
            v-auth="'shop.admin.commission.reward.export'"
            :loading="exportLoading"
            :disabled="exportLoading"
            @click="onExport"
            >导出</el-button
          >
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="state.loading">
      <el-table height="100%" class="sa-table" :data="state.tableData" stripe>
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column prop="id" label="ID" min-width="90" />
        <el-table-column label="订单号" min-width="260">
          <template #default="scope">
            {{ scope.row.order?.order_sn || scope.row.order_id }}
          </template>
        </el-table-column>
        <el-table-column label="下单用户" min-width="160">
          <template #default="scope">
            <sa-user-profile :user="scope.row.buyer" :id="scope.row.buyer_id" />
          </template>
        </el-table-column>
        <el-table-column label="分销用户" min-width="160">
          <template #default="scope">
            <sa-user-profile :user="scope.row.agent" :id="scope.row.agent_id" />
          </template>
        </el-table-column>
        <el-table-column label="分销金额" min-width="160" align="center">
          <template #default="scope"> {{ scope.row.commission }}元 </template>
        </el-table-column>
        <el-table-column label="入账状态" min-width="80">
          <template #default="scope">
            <span :class="`sa-color--${statusStyle[scope.row.status]}`">
              {{ scope.row.status_text }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="type_text" label="入账方式" min-width="80" align="center" />
        <el-table-column prop="commission_time" label="分佣时间" min-width="172" />
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script>
  export default {
    name: 'shop.admin.commission.reward',
  };
</script>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../commission.service';
  import { usePagination } from '@/sheep/hooks';
  import { useSearch } from '@/sheep/components/sa-table/sa-search/useSearch';
  import { composeFilter } from '@/sheep/utils';
  import { cloneDeep } from 'lodash';

  const statusStyle = {
    '-2': 'danger',
    '-1': 'warning',
    0: 'info',
    1: 'success',
  };

  const filterParams = reactive({
    tools: {
      buyer: {
        type: 'tinputprepend',
        label: '下单用户',
        placeholder: '请输入查询内容',
        field: 'buyer',
        buyer: {
          field: 'buyer_id',
          value: '',
        },
        options: [
          {
            label: '用户ID',
            value: 'buyer_id',
          },
          {
            label: '用户昵称',
            value: 'buyer.nickname',
          },
          {
            label: '用户手机号',
            value: 'buyer.mobile',
          },
        ],
      },
      agent: {
        type: 'tinputprepend',
        label: '结算分销商',
        placeholder: '请输入查询内容',
        field: 'agent',
        agent: {
          field: 'agent_id',
          value: '',
        },
        options: [
          {
            label: '结算ID',
            value: 'agent_id',
          },
          {
            label: '结算昵称',
            value: 'agent.nickname',
          },
          {
            label: '结算手机号',
            value: 'agent.mobile',
          },
        ],
      },
      'order.order_sn': {
        type: 'tinput',
        label: '订单号',
        placeholder: '请输入查询内容',
        field: 'order.order_sn',
        value: '',
      },
      commission_time: {
        type: 'tdatetimerange',
        label: '分佣时间',
        field: 'commission_time',
        value: [],
      },
      status: {
        type: 'tselect',
        label: '入账状态',
        field: 'status',
        value: '',
        options: {
          data: [
            {
              label: '已退回',
              value: '-2',
            },
            {
              label: '已取消',
              value: '-1',
            },
            {
              label: '未结算',
              value: '0',
            },
            {
              label: '已结算',
              value: '1',
            },
          ],
        },
      },
    },
    data: {
      buyer: { field: 'buyer_id', value: '' },
      agent: { field: 'agent_id', value: '' },
      'order.order_sn': '',
      commission_time: [],
      status: '',
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });

  const state = reactive({
    loading: false,
    tableData: [],
  });

  const { pageData } = usePagination();

  async function getData(page) {
    state.loading = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);
    let search = composeFilter(tempSearch, {
      'buyer.nickname': 'like',
      'buyer.mobile': 'like',
      'agent.nickname': 'like',
      'agent.mobile': 'like',
      'order.order_sn': 'like',
      commission_time: 'range',
    });

    const { error, data } = await api.reward.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      ...search,
    });
    if (error === 0) {
      state.tableData = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    state.loading = false;
  }

  const exportLoading = ref(false);
  async function onExport() {
    exportLoading.value = true;
    let tempSearch = cloneDeep(filterParams.data);
    let search = composeFilter(tempSearch, {
      'buyer.nickname': 'like',
      'buyer.mobile': 'like',
      'agent.nickname': 'like',
      'agent.mobile': 'like',
      'order.order_sn': 'like',
      commission_time: 'range',
    });
    await api.reward.export({ search: search.search });
    exportLoading.value = false;
  }

  onMounted(() => {
    getData();
  });
</script>
